package com.itmayiedu.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.itmayiedu.base.TextMessage;
import com.itmayiedu.utils.CheckUtil;
import com.itmayiedu.utils.HttpClientUtil;
import com.itmayiedu.utils.XmlUtils;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
public class DispatCherController {
	private static final String REQEST_HTTP = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=";
//	signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
//	timestamp	时间戳
//	nonce	随机数
//	echostr	随机字符串
	@RequestMapping(value="/dispatcher",method = RequestMethod.GET)
	public String dispatcher(String signature,String timestamp,String nonce,String echostr) {
		//1.验证参数
		boolean checkSignature = CheckUtil.checkSignature(signature, timestamp, nonce);
		//2.参数验证成功，返回随机数
		if(!checkSignature) {
			return null;
		}
		return echostr;
	}
	//微信动作推送
	@RequestMapping(value="/dispatcher",method = RequestMethod.POST)
	public void dispatcher(HttpServletRequest request,HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");	
		//1.将xml转换成map格式
		Map<String, String> resultmap = XmlUtils.parseXml(request);
		//2.判断消息类型
		String msgType = resultmap.get("MsgType");
		PrintWriter writer = response.getWriter();
		switch (msgType) {
		case "text":
			// 获取消息内容
			String content = resultmap.get("Content");
			// 发送消息
			String toUserName = resultmap.get("ToUserName");
			// 来自消息
			String fromUserName = resultmap.get("FromUserName");
			// 調用智能机器人接口
			String requestResultJson = HttpClientUtil.doGet(REQEST_HTTP + content);
			JSONObject jsonObject = new JSONObject().parseObject(requestResultJson);
			String result = jsonObject.getString("result");
			String msg = null;
			if (result.equals("0")) {
				msg = jsonObject.getString("content");
			} else {
				msg = "我也不知道回答什么！";
			}
			String resultTestMsg = setTextMess(msg, toUserName, fromUserName);
			writer.print(resultTestMsg);
			break;

		default:
			break;
		}
		writer.close();

		//3.如果文本类型，返回结果
		
	}
	public String setTextMess(String content, String fromUserName, String toUserName) {
		TextMessage textMessage = new TextMessage();
		textMessage.setFromUserName(fromUserName);
		textMessage.setToUserName(toUserName);
		textMessage.setContent(content);
		textMessage.setMsgType("text");
		textMessage.setCreateTime(new Date().getTime());
		String messageToXml = XmlUtils.messageToXml(textMessage);
		log.info("####setTextMess()###messageToXml:" + messageToXml);
		return messageToXml;
	}
}
